/**
 *  @file  vsDefines.h version 1.0 2007-11-20 
 *  @brief Macros for functions.
 *  Copyright (c) 2007 by @author <abitfree@gmail.com>
 */

#ifndef __VS_DEFINES_H__
#define __VS_DEFINES_H__

// c++ standard
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cstdarg>
#include <cstddef>
#include <cctype>
#include <cassert>

// platform related
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>	// for function access
#else	// WIN32
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#endif	// WIN32

namespace vs
{

/// The macro definition for inline key word.
#define INLINE inline

//@{
/** 
 * Values for the second argument of access function.
 * These flags can be used by OR together.
 */
#define R_OK    4               ///< Test for read permission.
#define W_OK    2               ///< Test for write permission.
#define X_OK    1               ///< Test for execute permission.
#define F_OK    0               ///< Test for existence.
//@}


// The windows macro GetCurrentTime is conflict with CTimeStamp::GetCurrentTime().
#ifdef GetCurrentTime
#undef GetCurrentTime
#endif


/// Disable the class copy functions.
#define DISABLE_CLASS_COPY(__class__) \
private: \
	inline __class__(const __class__&); \
	inline __class__& operator=(const __class__&)


/// A group of error handling macros.
#ifndef __VS_PRINT
#define __VS_PRINT printf
#endif

/// Exiting the program on error.
#define ERROR_EXIT(condition, err_val) \
	do { \
		if (!(condition)) \
		{ \
			__VS_PRINT("error happened when testing condition : " #condition " , exiting program ...\n"); \
			std::exit(err_val); \
		} \
	} while (0)

/// Aborting the program on error.
#define ERROR_ABORT(condition) \
	do { \
		if (!(condition)) \
		{ \
			__VS_PRINT("error happened when testing condition : " #condition " , aborting program ...\n"); \
			std::abort(); \
		} \
	} while (0)

/// Return err_val from function on error.
#define ERROR_RETURN_VALUE(condition, err_val) \
	do { \
		if (!(condition)) \
		{ \
			__VS_PRINT("error happened when testing condition : " #condition " , leaving the current function ...\n"); \
			return (err_val); \
		} \
	} while (0)

/// Return from function on error.
#define ERROR_RETURN(condition) \
	do { \
		if (!(condition)) \
		{ \
			__VS_PRINT("error happened when testing condition : " #condition " , leaving the current function ...\n"); \
			return; \
		} \
	} while (0)

/// Printing error msg, and exiting on error.
#define ERROR_REPROT(condition, msg) \
	do { \
		if (!(condition)) \
		{ \
			__VS_PRINT("%s\n", (msg)); \
			std::exit(0); \
		} \
	} while (0)

/// Return the number of elements in an array.
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))


} // namespace vs

#endif // __VS_DEFINES_H__
